home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / source / music4c.sit / Music4C Folder / SFConvert folder / floatTo.c < prev    next >
C/C++ Source or Header  |  1990-06-24  |  20KB  |  795 lines

  1. #include    "SFConvert.h"
  2. #include    <stdio.h>
  3. #include    <unix.h>
  4. #include    <string.h>
  5. #include    <math.h>
  6. #include    <SANE.h>
  7. #include    "SDtype.h"
  8. #include    "AIFFType.h"
  9.  
  10.  
  11.  
  12. extern    OSErr        theErr;
  13. extern    int            FileOpen;
  14. extern    long        TotalSamps;
  15. extern    CursHandle    watchCurs;
  16. extern    int            SoundFileType;
  17. extern    long        SampleRate;
  18. extern    long        NumChannels;
  19. extern    double        MaxSample;
  20. extern    double        MinSample;
  21. extern    int            nrec;
  22. extern    long        fileSize;
  23. extern    ioParam        myIOParmBlk;
  24. extern    ioParam        NewParmBlk;
  25. extern    int            SFOUTPUTtype;    
  26. extern    Boolean        SDnoResource;
  27. extern    long        RecLength;
  28. extern    Str255        NewSoundFileName;
  29.  
  30. Boolean    FloatToINT16(void);
  31. Boolean    FloatToCHUNKY(void);
  32. Boolean    FloatToSD1(void);
  33. Boolean    FloatToSD2(void);
  34. Boolean    FloatToAIFF(void);
  35.  
  36. extern    void    PstringCopy(char *, char *);
  37. extern    void    PstringCat(char *, char *);
  38. extern    void    DoOSErrorAlert(Str255, Str255);
  39.  
  40.  
  41. Boolean    FloatToINT16()
  42. {
  43.     register    long    i;
  44.     register    double    offset;
  45.     register    double    scalefactor;
  46.     register    double    x;
  47.     Str255    mess;
  48.     int        *theIbuf, *Iptr;
  49.     float        *sp, *SampBuf;
  50.     long        nBytes;
  51.     long        nSamps;
  52.     long        bytesLeft;
  53.     long        sampBufsz;
  54.  
  55.     
  56.     RecLength = (long)(16384);
  57.  
  58.     nrec = 0;
  59.     SetProgressDialog();
  60.  
  61.     TotalSamps =  fileSize / sizeof(float);
  62.     myIOParmBlk.ioReqCount = TotalSamps * sizeof(int);
  63.     myIOParmBlk.ioCompletion = NIL;
  64.     if ( (theErr = PBAllocContig(&myIOParmBlk, FALSE)) != noErr ) {
  65.         if ( theErr == dskFulErr ) {
  66.             if ( (theErr = PBAllocate(&myIOParmBlk, FALSE)) != noErr ) {
  67.                 DoOSErrorAlert("\pCan't allocate diskspace for contig file", NIL);
  68.             }
  69.         }
  70.     }
  71.     
  72.     sampBufsz  = RecLength  / sizeof(float);
  73.     theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
  74.     SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
  75.     
  76.     
  77. /*    offset = fabs(MinSample);
  78.     scalefactor = SAMPMAX / (offset + MaxSample);
  79. */
  80.     scalefactor = fabs(MinSample);
  81.     if ( fabs(MaxSample) > fabs(MinSample))
  82.         scalefactor = (SAMPMAX / 2.0) / fabs(MaxSample);
  83.     else
  84.         scalefactor = (SAMPMAX / 2.0) / fabs(MinSample);
  85.  
  86.  
  87.     bytesLeft = fileSize;
  88.  
  89.     myIOParmBlk.ioPosMode = fsAtMark;
  90.     myIOParmBlk.ioPosOffset = NIL;
  91.  
  92.     NewParmBlk.ioPosMode = fsAtMark;
  93.     NewParmBlk.ioPosOffset = NIL;
  94.  
  95.  
  96.     while ( bytesLeft > 0L ) {
  97.         if ( bytesLeft > RecLength )
  98.             nBytes = RecLength;
  99.         else
  100.             nBytes = bytesLeft;
  101.             
  102.         myIOParmBlk.ioBuffer = (Ptr)SampBuf;
  103.         myIOParmBlk.ioReqCount = nBytes;
  104.         theErr = PBRead(&myIOParmBlk, FALSE);
  105.         if (theErr != noErr ) {
  106.             if ( theErr == eofErr )
  107.                 DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
  108.         }
  109.         nBytes = myIOParmBlk.ioActCount;
  110.         nSamps = nBytes / sizeof(float);
  111.         for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
  112.             x = *sp++ * scalefactor;
  113.             *Iptr++ = (int)x;
  114.         }
  115.  
  116. /* write it out */
  117.         NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
  118.         NewParmBlk.ioBuffer = (Ptr)theIbuf;
  119.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  120.             DoOSErrorAlert("\pError writing to sample file", NIL);
  121.         }
  122.         if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
  123.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  124.         }
  125.  
  126.         nrec++;
  127.         if ( !UpdateProgressDialog() ) {
  128.             DisposPtr((Ptr)SampBuf);
  129.             DisposPtr((Ptr)theIbuf);
  130.             DisposeProgDialog();
  131.             InitCursor();
  132.             return(FALSE);
  133.         }
  134.         bytesLeft -= nBytes;
  135.     }
  136.     DisposPtr((Ptr)SampBuf);    
  137.     DisposPtr((Ptr)theIbuf);    
  138.     DisposeProgDialog();
  139.     InitCursor();
  140.     return(TRUE);
  141. }
  142.  
  143.  
  144. Boolean    FloatToCHUNKY()
  145. {
  146.     register    long    i;
  147.     register    double    offset;
  148.     register    double    scalefactor;
  149.     register    double    x;
  150.     Str255        mess;
  151.     unsigned    int        *theIbuf, *Iptr;
  152.     float        *sp, *SampBuf;
  153.     long        nBytes;
  154.     long        nSamps;
  155.     long        bytesLeft;
  156.     long        sampBufsz;
  157.     unsigned char        *highBuf, *h, *lowBuf, *l;
  158.     unsigned char        *RhighBuf, *Rh, *RlowBuf, *Rl;
  159.     
  160.     
  161.     
  162.     RecLength = (long)(32768);
  163.     
  164.     nrec = 0;
  165.         SetProgressDialog();
  166.  
  167.     TotalSamps =  fileSize / sizeof(float);
  168.     
  169.     sampBufsz  = RecLength  / sizeof(float);
  170.     theIbuf = (unsigned int *)NewPtr(sizeof(int) * sampBufsz);
  171.     if ( (theErr = MemError()) != noErr ) {
  172.         DoOSErrorAlert("\pcan't get enough memory  for theIbuf", NIL);
  173.         return(FALSE);
  174.     }
  175.     SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
  176.     if ( (theErr = MemError()) != noErr ) {
  177.         DoOSErrorAlert("\pcan't get enough memory  for SampBuf", NIL);
  178.         return(FALSE);
  179.     }
  180.     highBuf = (unsigned char *)NewPtr(sampBufsz);
  181.     if ( (theErr = MemError()) != noErr ) {
  182.         DoOSErrorAlert("\pcan't get enough memory  for highBuf", NIL);
  183.         return(FALSE);
  184.     }
  185.     lowBuf = (unsigned char *)NewPtr(sampBufsz);
  186.     if ( (theErr = MemError()) != noErr ) {
  187.         DoOSErrorAlert("\pcan't get enough memory  for lowBuf", NIL);
  188.         return(FALSE);
  189.     }
  190.     if ( NumChannels == 2 ) {
  191.         RhighBuf = (unsigned char *)NewPtr(sampBufsz);
  192.         if ( (theErr = MemError()) != noErr ) {
  193.             DoOSErrorAlert("\pcan't get enough memory  for RhighBuf", NIL);
  194.             return(FALSE);
  195.         }
  196.         RlowBuf = (unsigned char *)NewPtr(sampBufsz);
  197.         if ( (theErr = MemError()) != noErr ) {
  198.             DoOSErrorAlert("\pcan't get enough memory  for RlowBuf", NIL);
  199.             return(FALSE);
  200.         }
  201.     }
  202.     
  203.     offset = fabs(MinSample);
  204.     scalefactor = SAMPMAX / (offset + MaxSample);
  205.  
  206.  
  207.     bytesLeft = fileSize;
  208.  
  209.     myIOParmBlk.ioPosMode = fsAtMark;
  210.     myIOParmBlk.ioPosOffset = NIL;
  211.  
  212.     NewParmBlk.ioPosMode = fsAtMark;
  213.     NewParmBlk.ioPosOffset = NIL;
  214.  
  215.  
  216.     while ( bytesLeft > 0L ) {
  217.         if ( bytesLeft > RecLength )
  218.             nBytes = RecLength;
  219.         else
  220.             nBytes = bytesLeft;
  221.             
  222.         myIOParmBlk.ioBuffer = (Ptr)SampBuf;
  223.         myIOParmBlk.ioReqCount = nBytes;
  224.         theErr = PBRead(&myIOParmBlk, FALSE);
  225.         if (theErr != noErr ) {
  226.             if ( theErr == eofErr )
  227.                 DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
  228.         }
  229.         nBytes = myIOParmBlk.ioActCount;
  230.         nSamps = nBytes / sizeof(float);
  231.         for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
  232.             x = ((*sp++ + offset ) * scalefactor);
  233.             *Iptr++ = (unsigned int)x;
  234.         }
  235.     /* convert to CHUNKY format */
  236.         Iptr = theIbuf;
  237.         h = highBuf;
  238.         l = lowBuf;
  239.         if ( NumChannels == 2 ) {
  240.             Rh = RhighBuf;
  241.             Rl = RlowBuf;
  242.         }
  243.         for ( i = 0; i < nSamps; i++ ) {
  244.             *h++ = (unsigned char)((*Iptr & 0xFF00) >> 8);
  245.             *l++ = (unsigned char )(*Iptr & 0x00FF);
  246.             Iptr++;
  247.             if ( NumChannels == 2) {
  248.                 *Rh++ = (unsigned char)(*Iptr & 0xFF00) >> 8;
  249.                 *Rl++ = (unsigned char)*Iptr & 0x00FF;
  250.                 Iptr++;
  251.             }
  252.         }
  253.  
  254. /* write it out */
  255.         NewParmBlk.ioReqCount = (long)nSamps;
  256.         NewParmBlk.ioBuffer = (Ptr)highBuf;
  257.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  258.             DoOSErrorAlert("\pError writing to sample file", NIL);
  259.         }
  260.         if ( NewParmBlk.ioActCount != (long)(nSamps) ) {
  261.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  262.         }
  263.  
  264.         NewParmBlk.ioBuffer = (Ptr)lowBuf;
  265.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  266.             DoOSErrorAlert("\pError writing to sample file", NIL);
  267.         }
  268.         if ( NewParmBlk.ioActCount != (long)(nSamps) ) {
  269.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  270.         }
  271.  
  272.         if ( NumChannels == 2 ) {
  273.             NewParmBlk.ioReqCount = (long)nSamps;
  274.             NewParmBlk.ioBuffer = (Ptr)RhighBuf;
  275.             if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  276.                 DoOSErrorAlert("\pError writing to sample file", NIL);
  277.             }
  278.             if ( NewParmBlk.ioActCount != (long)(nSamps) ) {
  279.                 DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  280.             }
  281.     
  282.             NewParmBlk.ioBuffer = (Ptr)RlowBuf;
  283.             if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  284.                 DoOSErrorAlert("\pError writing to sample file", NIL);
  285.             }
  286.             if ( NewParmBlk.ioActCount != (long)(nSamps) ) {
  287.                 DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  288.             }
  289.         }
  290.         nrec++;
  291.         if ( !UpdateProgressDialog() ) {
  292.             DisposPtr((Ptr)SampBuf);
  293.             DisposPtr((Ptr)theIbuf);
  294.             DisposeProgDialog();
  295.             InitCursor();
  296.             return(FALSE);
  297.         }
  298.         bytesLeft -= nBytes;
  299.     }
  300.     DisposPtr((Ptr)SampBuf);    
  301.     DisposPtr((Ptr)theIbuf);    
  302.     DisposeProgDialog();
  303.     InitCursor();
  304.     return(TRUE);
  305. }
  306.  
  307. Boolean    FloatToSD1()
  308. {
  309.     register    long    i;
  310.     register    double    scalefactor;
  311.     register    double    x;
  312.     double        peak;
  313.     Str255        mess;
  314.     int            *theIbuf, *Iptr;
  315.     float        *sp, *SampBuf;
  316.     long        nBytes;
  317.     long        nSamps;
  318.     long        bytesLeft;
  319.     long        sampBufsz;
  320.     WaveRec    Header;
  321.     WavPtr    HeaderPtr;
  322.     
  323.     
  324.     RecLength = (long)(16384);
  325.     HeaderPtr = &Header;
  326.     
  327.     TotalSamps =  fileSize / sizeof(float);
  328.     
  329.     
  330.     
  331.     
  332. /* initialize Header info to default values */
  333.     Header.HeaderSize = 1336;
  334.     Header.Version = 0;
  335.     Header.Preview =     0;
  336.     Header.WPtr = 0L;    
  337.     Header.WPeek = 0L;
  338.     Header.HInxPage = 0L;
  339.     Header.HInxLine = 0L;
  340.     Header.VInxPage = 0L;
  341.     Header.VInxLine = 0L;
  342.     Header.HCtlPage = 0L;    
  343.     Header.HCtlLine =  0L;
  344.     Header.VCtlPage = 255L;
  345.     Header.VCtlLine = 65536L;
  346.     Header.VOffset = 0L;    
  347.     Header.HOffset = 0L;
  348.     Header.VOffConst = -1L;
  349.     Header.ZF.v = -256;
  350.     Header.ZF.h = -128L;    
  351.     Header.SF.VFactor =     327L;
  352.     Header.SF.VType =     1032;
  353.     PstringCopy(&Header.SF.VString, "\p%Scale");
  354.     Header.SF.HFactor =     1;
  355.     Header.SF.HType = 30L;
  356.     PstringCopy(&Header.SF.HString, "\p  secs");
  357.     Header.VScrUpdate = 0;
  358.     Header.BufPtr = NIL;
  359.     Header.BufBytes = 0L;
  360.     Header.BufOffset = 0L;
  361.     Header.WaveRgn = 0L;
  362. /*    (**Header.WaveRgn).rgnSize = 0L;
  363.     (**Header.WaveRgn).rgnBBox.top = 0;
  364.     (**Header.WaveRgn).rgnBBox.left = 16512;
  365.     (**Header.WaveRgn).rgnBBox.bottom = 10772;
  366.     (**Header.WaveRgn).rgnBBox.right = 58;
  367. */
  368.     Header.ClipArea = 0L;
  369.     Header.ScaleArea = 0L;
  370.     Header.CtlWidth.top = 0;
  371.     Header.CtlWidth.left = 29;
  372.     Header.CtlWidth.bottom = 33;
  373.     Header.CtlWidth.right = 15;
  374.     Header.VScroll = NIL;
  375.     Header.HScroll = NIL;
  376.  
  377.     Header.FileSize = (long)(TotalSamps * sizeof(int));
  378.     PstringCopy(&Header.BUName, "\p");            
  379.     PstringCopy(&Header.FileName, NewSoundFileName);            
  380.     Header.BURefNum = 0;            
  381.     Header.refNum = NewParmBlk.ioRefNum;                        
  382.     Header.vRefNum = NewParmBlk.ioVRefNum;
  383.     Header.BufChanged = 0;
  384.     Header.FileChanged = 0;
  385.     Header.NoBackup = 0;
  386.     Header.Mode = 0;
  387.     Header.Edit.HiAddr = 0L;
  388.     Header.Edit.LoAddr = 0L;
  389.     Header.Edit.ExtendSide = 0;
  390.     Header.CursorPos = 0L;
  391.     Header.CursorRgn =     0L;
  392.     for ( i = 0; i < 10; i++ ) {
  393.         Header.MarkerData[i].Free = 1;
  394.         Header.MarkerData[i].Position = 0L;
  395.         PstringCopy(&Header.MarkerData[i].Name, "\p");
  396.     }
  397.     Header.MarkerOffset = 0L;
  398.     Header.LoopStart =     -1L;
  399.     Header.LoopEnd =     -1L;
  400.     Header.ZeroLineOn = 0;
  401.     Header.CursorOn = 0;
  402.     Header.ScalesOn = 511;
  403.     PstringCopy(&Header.Comment, "\p");
  404.     Header.SampRate =     SampleRate;
  405.     x = (1.0/(double)SampleRate) * 1000000.0;
  406.     Header.SampPeriod = (long)(x + 0.5);
  407.     Header.SampSize = 16;
  408.     PstringCopy(&Header.CodeType, "\pLinear");
  409.     PstringCopy(&Header.UserStr1, "\p");
  410.     Header.BufSize = 0L;
  411.     Header.Loop2Start = -1L;
  412.     Header.Loop2End = -1L;
  413.     Header.Loop1Type = 1;
  414.     Header.Loop2Type = 1;
  415.     Header.User4 = 0;
  416.  
  417. /* write out header infor */
  418.     NewParmBlk.ioReqCount = Header.HeaderSize;
  419.     NewParmBlk.ioBuffer = (Ptr)HeaderPtr;
  420.     if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  421.         DoOSErrorAlert("\pError writing Header to sample file", NIL);
  422.     }
  423.     if ( NewParmBlk.ioActCount != Header.HeaderSize) {
  424.         DoOSErrorAlert("\pError writing Header to file, wrote wrong number of bytes", NIL);
  425.     }
  426.  
  427.  
  428.  
  429.     
  430.     nrec = 0;
  431.     SetProgressDialog();
  432.  
  433.     
  434.     sampBufsz  = RecLength  / sizeof(float);
  435.     theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
  436.     SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
  437.     
  438.     
  439. /*    offset = fabs(MinSample);
  440.     scalefactor = SAMPMAX / (offset + MaxSample);
  441. */
  442.  
  443.     peak = MAX(fabs(MinSample), fabs(MaxSample));
  444.     scalefactor = (SAMPMAX / 2.0) / peak;
  445.     bytesLeft = fileSize;
  446.  
  447.     myIOParmBlk.ioPosMode = fsAtMark;
  448.     myIOParmBlk.ioPosOffset = NIL;
  449.  
  450.     NewParmBlk.ioPosMode = fsAtMark;
  451.     NewParmBlk.ioPosOffset = NIL;
  452.  
  453.  
  454.     while ( bytesLeft > 0L ) {
  455.         if ( bytesLeft > RecLength )
  456.             nBytes = RecLength;
  457.         else
  458.             nBytes = bytesLeft;
  459.             
  460.         myIOParmBlk.ioBuffer = (Ptr)SampBuf;
  461.         myIOParmBlk.ioReqCount = nBytes;
  462.         theErr = PBRead(&myIOParmBlk, FALSE);
  463.         if (theErr != noErr ) {
  464.             if ( theErr == eofErr )
  465.                 DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
  466.         }
  467.         nBytes = myIOParmBlk.ioActCount;
  468.         nSamps = nBytes / sizeof(float);
  469.         for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
  470. /*            x = ((*sp++ + offset ) * scalefactor);*/
  471.             x = *sp++ * scalefactor;
  472.             if ( x < MinSample )
  473.                 MinSample = x;
  474.             if ( x > MaxSample )
  475.                 MaxSample = x;
  476.             *Iptr++ = (int)x;
  477.         }
  478.  
  479. /* write it out */
  480.         NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
  481.         NewParmBlk.ioBuffer = (Ptr)theIbuf;
  482.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  483.             DoOSErrorAlert("\pError writing to sample file", NIL);
  484.         }
  485.         if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
  486.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  487.         }
  488.  
  489.         nrec++;
  490.         if ( !UpdateProgressDialog() ) {
  491.             DisposPtr((Ptr)SampBuf);
  492.             DisposPtr((Ptr)theIbuf);
  493.             DisposeProgDialog();
  494.             InitCursor();
  495.             return(FALSE);
  496.         }
  497.         bytesLeft -= nBytes;
  498.     }
  499.     DisposPtr((Ptr)SampBuf);    
  500.     DisposPtr((Ptr)theIbuf);    
  501.     DisposeProgDialog();
  502.     InitCursor();
  503.     return(TRUE);
  504. }
  505.  
  506. Boolean    FloatToSD2()
  507. {
  508.     register    long    i;
  509.     register    double    scalefactor;
  510.     register    double    x;
  511.     double        peak;
  512.     Str255        mess;
  513.     int            *theIbuf, *Iptr;
  514.     float        *sp, *SampBuf;
  515.     long        nBytes;
  516.     long        nSamps;
  517.     long        bytesLeft;
  518.     long        sampBufsz;
  519.     
  520.     
  521.     RecLength = (long)(16384);
  522.     
  523.     
  524. /* SD 2 format - just like SD1 soundfile data part, do resources later */
  525.  
  526.     nrec = 0;
  527.     SetProgressDialog();
  528.     
  529.     sampBufsz  = RecLength  / sizeof(float);
  530.     theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
  531.     SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
  532.     
  533.     
  534.     peak = MAX(fabs(MinSample), fabs(MaxSample));
  535.     scalefactor = (SAMPMAX / 2.0) / peak;
  536.     bytesLeft = fileSize;
  537.  
  538.     myIOParmBlk.ioPosMode = fsAtMark;
  539.     myIOParmBlk.ioPosOffset = NIL;
  540.  
  541.     NewParmBlk.ioPosMode = fsAtMark;
  542.     NewParmBlk.ioPosOffset = NIL;
  543.  
  544.  
  545.     while ( bytesLeft > 0L ) {
  546.         if ( bytesLeft > RecLength )
  547.             nBytes = RecLength;
  548.         else
  549.             nBytes = bytesLeft;
  550.             
  551.         myIOParmBlk.ioBuffer = (Ptr)SampBuf;
  552.         myIOParmBlk.ioReqCount = nBytes;
  553.         theErr = PBRead(&myIOParmBlk, FALSE);
  554.         if (theErr != noErr ) {
  555.             if ( theErr == eofErr )
  556.                 DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
  557.         }
  558.         nBytes = myIOParmBlk.ioActCount;
  559.         nSamps = nBytes / sizeof(float);
  560.         for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
  561. /*            x = ((*sp++ + offset ) * scalefactor);*/
  562.             x = *sp++ * scalefactor;
  563.             if ( x < MinSample )
  564.                 MinSample = x;
  565.             if ( x > MaxSample )
  566.                 MaxSample = x;
  567.             *Iptr++ = (int)x;
  568.         }
  569.  
  570. /* write it out */
  571.         NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
  572.         NewParmBlk.ioBuffer = (Ptr)theIbuf;
  573.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  574.             DoOSErrorAlert("\pError writing to sample file", NIL);
  575.         }
  576.         if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
  577.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  578.         }
  579.  
  580.         nrec++;
  581.         if ( !UpdateProgressDialog() ) {
  582.             DisposPtr((Ptr)SampBuf);
  583.             DisposPtr((Ptr)theIbuf);
  584.             DisposeProgDialog();
  585.             InitCursor();
  586.             return(FALSE);
  587.         }
  588.         bytesLeft -= nBytes;
  589.     }
  590.     DisposPtr((Ptr)SampBuf);    
  591.     DisposPtr((Ptr)theIbuf);    
  592.     DisposeProgDialog();
  593.     InitCursor();
  594.     return(TRUE);
  595. }
  596.  
  597. Boolean    FloatToAIFF()
  598. {
  599.     register    long    i;
  600.     register    double    scalefactor;
  601.     register    double    x;
  602.     double        peak;
  603.     int            *theIbuf, *Iptr;
  604.     float        *sp, *SampBuf;
  605.     long        nBytes;
  606.     long        nSamps;
  607.     long        bytesLeft;
  608.     long        sampBufsz;
  609.  
  610.  
  611.     Chunk            FormChunkHeader, *FormChunkHeaderPtr;
  612.     CommonChunk        CommonHeader, *CommonHeaderPtr;
  613.     SoundDataChunk    SoundDataHeader, *SoundDataHeaderPtr;
  614.     MarkerChunk        MarkerChunkHeader, *MarkerChunkHeaderPtr;
  615.     InstrumentChunk        InstHeader, *InstHeaderPtr;
  616.     Boolean    oddByte;
  617.     int theInt;
  618.     int theChar;
  619.     double    aDouble;
  620.     int    kk;
  621.     Str255    myStr255;
  622.  
  623.  
  624.     RecLength = (long)(16384);
  625.     FormChunkHeaderPtr = &FormChunkHeader;
  626.     CommonHeaderPtr = &CommonHeader;
  627.     SoundDataHeaderPtr = &SoundDataHeader;
  628.     TotalSamps =  fileSize / sizeof(float);
  629. /* initialize Form Chunk info */
  630.     FormChunkHeader.ckID = 'FORM';
  631.     FormChunkHeader.formType = 'AIFF';
  632.  
  633.  
  634. /* initialize Common Chunk info */
  635.     CommonHeader.ckID = 'COMM';
  636.     CommonHeader.ckSize = sizeof( CommonHeader.numChannels ) +
  637.                             sizeof( CommonHeader.numSampleFrames ) +
  638.                             sizeof( CommonHeader.sampleSize ) +
  639.                             sizeof( CommonHeader.sampleRate );
  640.     CommonHeader.numChannels = (short)NumChannels;
  641.     CommonHeader.numSampleFrames = (unsigned long)(TotalSamps / NumChannels);
  642.     CommonHeader.sampleSize = SixteenBits;
  643.  
  644.     NumToString(SampleRate, &myStr255);
  645.     CommonHeader.sampleRate = str2num(&myStr255);    /* string to extended */
  646.     
  647.     
  648.  
  649.  
  650.     SoundDataHeader.ckID = 'SSND';
  651.     SoundDataHeader.offset = 0L;
  652.     SoundDataHeader.blockSize = 0L;
  653.  
  654.     SoundDataHeader.ckSize = sizeof( SoundDataHeader.offset ) +
  655.                                 sizeof( SoundDataHeader.blockSize ) +
  656.                                 (long)(TotalSamps * sizeof(int));
  657.                                 
  658.                                 
  659.                                 
  660.     FormChunkHeader.ckSize = sizeof(FormChunkHeader.formType);
  661.     
  662.     FormChunkHeader.ckSize += (sizeof(CommonHeader.ckID) + 
  663.                             sizeof(CommonHeader.ckSize)
  664.                                 + CommonHeader.ckSize);
  665.                                 
  666.     FormChunkHeader.ckSize += (sizeof(SoundDataHeader.ckID) + 
  667.                                 sizeof(SoundDataHeader.ckSize)
  668.                                 + SoundDataHeader.ckSize);
  669.                                 
  670.     if ( FormChunkHeader.ckSize % 2 != 0 )
  671.         oddByte = TRUE;
  672.     else
  673.         oddByte = FALSE;
  674.  
  675.                                 
  676. /* write out header info */
  677.     NewParmBlk.ioReqCount = sizeof(FormChunkHeader);
  678.     NewParmBlk.ioBuffer = (Ptr)FormChunkHeaderPtr;
  679.     if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  680.         DoOSErrorAlert("\pError writing Header to sample file", NIL);
  681.     }
  682.     if ( NewParmBlk.ioActCount != NewParmBlk.ioReqCount) {
  683.         DoOSErrorAlert("\pError writing Header to file, wrote wrong number of bytes", NIL);
  684.     }
  685.  
  686.     NewParmBlk.ioReqCount = sizeof(CommonHeader);
  687.     NewParmBlk.ioBuffer = (Ptr)CommonHeaderPtr;
  688.     if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  689.         DoOSErrorAlert("\pError writing Header to sample file", NIL);
  690.     }
  691.     if ( NewParmBlk.ioActCount != NewParmBlk.ioReqCount) {
  692.         DoOSErrorAlert("\pError writing Header to file, wrote wrong number of bytes", NIL);
  693.     }
  694.  
  695.     NewParmBlk.ioReqCount = sizeof(SoundDataHeader);
  696.     NewParmBlk.ioBuffer = (Ptr)SoundDataHeaderPtr;
  697.     if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  698.         DoOSErrorAlert("\pError writing Header to sample file", NIL);
  699.     }
  700.     if ( NewParmBlk.ioActCount != NewParmBlk.ioReqCount) {
  701.         DoOSErrorAlert("\pError writing Header to file, wrote wrong number of bytes", NIL);
  702.     }
  703.  
  704.     nrec = 0;
  705.     SetProgressDialog();
  706.  
  707.     
  708.     sampBufsz  = RecLength  / sizeof(float);
  709.     theIbuf = (int *)NewPtr(sizeof(int) * sampBufsz);
  710.     SampBuf = (float *)NewPtr( sizeof(float) * sampBufsz);
  711.     
  712.     
  713. /*    offset = fabs(MinSample);
  714.     scalefactor = SAMPMAX / (offset + MaxSample);
  715. */
  716.  
  717.     peak = MAX(fabs(MinSample), fabs(MaxSample));
  718.     scalefactor = (SAMPMAX / 2.0) / peak;
  719.     bytesLeft = fileSize;
  720.  
  721.     myIOParmBlk.ioPosMode = fsAtMark;
  722.     myIOParmBlk.ioPosOffset = NIL;
  723.  
  724.     NewParmBlk.ioPosMode = fsAtMark;
  725.     NewParmBlk.ioPosOffset = NIL;
  726.  
  727.  
  728.     while ( bytesLeft > 0L ) {
  729.         if ( bytesLeft > RecLength )
  730.             nBytes = RecLength;
  731.         else
  732.             nBytes = bytesLeft;
  733.             
  734.         myIOParmBlk.ioBuffer = (Ptr)SampBuf;
  735.         myIOParmBlk.ioReqCount = nBytes;
  736.         theErr = PBRead(&myIOParmBlk, FALSE);
  737.         if (theErr != noErr ) {
  738.             if ( theErr == eofErr )
  739.                 DoOSErrorAlert("\pEnd of file reached in read form MACread_set", NIL);
  740.         }
  741.         nBytes = myIOParmBlk.ioActCount;
  742.         nSamps = nBytes / sizeof(float);
  743.         for ( i = 0, Iptr = theIbuf, sp = SampBuf; i < nSamps; i++ ) {
  744. /*            x = ((*sp++ + offset ) * scalefactor);*/
  745. /*            x = *sp++ * scalefactor;*/
  746.             x = *sp++;
  747.             x *= scalefactor;
  748.             if ( x < MinSample )
  749.                 MinSample = x;
  750.             if ( x > MaxSample )
  751.                 MaxSample = x;
  752.             kk = (int)x;
  753.             *Iptr++ = (int)x;
  754.         }
  755.  
  756. /* write it out */
  757.         NewParmBlk.ioReqCount = (long)(nSamps * sizeof(int));
  758.         NewParmBlk.ioBuffer = (Ptr)theIbuf;
  759.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  760.             DoOSErrorAlert("\pError writing to sample file", NIL);
  761.         }
  762.         if ( NewParmBlk.ioActCount != (long)(nSamps * sizeof(int)) ) {
  763.             DoOSErrorAlert("\pError writing to sample file, wrote wrong number of bytes", NIL);
  764.         }
  765.  
  766.         nrec++;
  767.         if ( !UpdateProgressDialog() ) {
  768.             DisposPtr((Ptr)SampBuf);
  769.             DisposPtr((Ptr)theIbuf);
  770.             DisposeProgDialog();
  771.             InitCursor();
  772.             return(FALSE);
  773.         }
  774.         bytesLeft -= nBytes;
  775.     }
  776.     
  777.     if ( oddByte ) {
  778.         NewParmBlk.ioReqCount = 1L;
  779.         theChar = '\0';
  780.         NewParmBlk.ioBuffer = (Ptr)theChar;
  781.         if ( (theErr = PBWrite(&NewParmBlk, FALSE)) != noErr ) { 
  782.             DoOSErrorAlert("\pError writing to sample file", NIL);
  783.         }
  784.     }
  785.     
  786.     
  787.     
  788.     
  789.     
  790.     DisposPtr((Ptr)SampBuf);    
  791.     DisposPtr((Ptr)theIbuf);    
  792.     DisposeProgDialog();
  793.     InitCursor();
  794.     return(TRUE);
  795. }